package com.example.smartcity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 注册 activity
 *
 * @author YSJ
 * @date 2021/4/17 : 13:41
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        findViewById(R.id.button).setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        EditText userName = findViewById(R.id.userName);
        String myUserName = userName.getText().toString();

        EditText nickName = findViewById(R.id.nickName);
        String mynickName = nickName.getText().toString();

        EditText phonenumber = findViewById(R.id.phonenumber);
        String myphonenumber = phonenumber.getText().toString();

        EditText sex = findViewById(R.id.sex);
        String mysex = sex.getText().toString();

        EditText password = findViewById(R.id.password);
        String mypassword = password.getText().toString();


        if (myUserName.equals("")) {
            Toast.makeText(RegisterActivity.this, "请填写 UserName", Toast.LENGTH_SHORT).show();
            return;
        }

        if (mynickName.equals("")) {
            Toast.makeText(RegisterActivity.this, "请填写 mynickName", Toast.LENGTH_SHORT).show();
            return;
        }

        if (myphonenumber.equals("")) {
            Toast.makeText(RegisterActivity.this, "请填写 myphonenumber", Toast.LENGTH_SHORT).show();
            return;
        }

        if (mysex.equals("")) {
            Toast.makeText(RegisterActivity.this, "请填写 mysex", Toast.LENGTH_SHORT).show();
            return;
        }

        if (mypassword.equals("")) {
            Toast.makeText(RegisterActivity.this, "请填写 mypassword", Toast.LENGTH_SHORT).show();
            return;
        }


        //发起网络请求
        //考虑线程 ==

        //客户端
        OkHttpClient client = new OkHttpClient();


        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("userName", myUserName);
            jsonObject.put("nickName", mynickName);
            jsonObject.put("phonenumber", myphonenumber);
            jsonObject.put("sex", mysex);
            jsonObject.put("password", mypassword);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String s = jsonObject.toString();


        //请求体
        MediaType mediaType = MediaType.get("application/json; charset=utf-8");

        RequestBody body = RequestBody.create(s, mediaType);

        Request request = new Request.Builder()
                .url("http://124.93.196.45:10002/system/user/register")
                .post(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RegisterActivity.this,
                                e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                try {
                    JSONObject jsonObject1 = new JSONObject(string);
                    int code = (int) jsonObject1.opt("code");
                    if (code == 200) {
                        //注册成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });

                    } else {
                        //注册失败
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


    }
}